﻿namespace Services.Client;

/// <summary>
/// 月产品
/// </summary>
public class MonthproductServices : BaseServices
{
    private readonly ISqlSugarClient db;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="db"></param>
    public MonthproductServices(ISqlSugarClient db)
    {
        this.db = db;
    }

    /// <summary>
    /// 获取产品列表
    /// </summary>
    /// <returns></returns>
    public async Task<object> GetProducts()
    {
        var data = await db.Queryable<Entity.MonthProduct>()

            .ToPagedListAsync(PageNo, PageSize);

        return Ok(data);
    }

    /// <summary>
    /// 下单
    /// </summary>
    /// <param name="productId"></param>
    /// <param name="addressId"></param>
    /// <returns></returns>
    public async Task<object> CreatedOrder(int productId, int addressId)
    {
        var product = await db.Queryable<Entity.MonthProduct>().FirstAsync(x => x.Id == productId);

        string orderNo = Extends.GetOrderNo();

        try
        {
            await db.Ado.BeginTranAsync();

            var order = new Entity.Order
            {
                OldPrice = product.MarketPrice + product.Postage,

                PayPrice = product.Postage,

                OrderNo = orderNo,

                OrderType = Entity.OrderType.每月免费商品,

                UserId = USERID
            };

            int orderId = await db.Insertable(order).ExecuteReturnIdentityAsync();

            var orderExt = new Entity.Orders.Order_Ext_MonthProduct
            {
                AddressId = addressId,

                OrderId = orderId,

                ProductId = productId,

                ExpressId = 0,

                UserId = USERID
            };

            await db.Insertable(orderExt).ExecuteReturnIdentityAsync();

            await db.Ado.CommitTranAsync();

            return Ok(orderNo);
        }
        catch (Exception)
        {
            await db.Ado.RollbackTranAsync();

            return No("下单失败,请重新下单");
        }

    }

    /// <summary>
    /// 获取月产品订单
    /// </summary>
    /// <returns></returns>
    public async Task<object> GetOrders([FromQuery] int state)
    {
        var list = await db.Queryable<Entity.Orders.Order_Ext_MonthProduct>()

            .Where(x => x.UserId == USERID)

            .Includes(x => x.ProdcutInfo)

            .Includes(x => x.OrderInfo)

            .Includes(x => x.ExpressInfo)

            .Where(x=>x.OrderInfo.PayStatus == (Entity.PayStatus)state)

            .ToPagedListAsync(PageNo, PageSize);

        return Ok(list);
    }

    /// <summary>
    /// 获取订单列表
    /// </summary>
    /// <param name="state"></param>
    /// <returns></returns>
    public async Task<object> GetOrderList(int state)
    {
        var list = await db.Queryable<Entity.Order>()

            .Where(x => x.UserId == USERID && x.OrderType == Entity.OrderType.每月免费商品)

            .ToPagedListAsync(PageNo, PageSize);

        return Ok(list);
    }
}
